package cn.ll.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ll.po.Role;
import cn.ll.po.User;
import cn.ll.po.UserRole;
import cn.ll.service.RoleService;
import cn.ll.service.UserRoleService;
import cn.ll.service.UserService;

public class UserRoleActino extends Action {

		public void save(HttpServletRequest request,HttpServletResponse response){
			String uid = request.getParameter("uid");
			String roleid = request.getParameter("roleid");
			
			if(uid != null && uid.length() > 0 && roleid != null && roleid.length() > 0){
				UserService userService = (UserService) this.getApplicationContext().getBean("userService");
				RoleService roleService = (RoleService) this.getApplicationContext().getBean("roleService");
				UserRoleService userRoleService = (UserRoleService)this.getApplicationContext().getBean("userRoleService");
				User user = userService.findById(uid);
				Role role = roleService.findById(roleid);
				if(user != null && role != null){
					UserRole userRole = new UserRole();
					userRole.setRoleid(role.getId());
					userRole.setUid(user.getId());
					
					userRoleService.save(userRole);
					
					Map rs = new HashMap();
					rs.put("success", true);
					rtn(rs, request, response);
					return;
				}
			}
			
			Map rs = new HashMap();
			rs.put("success", false);
			rtn(rs, request, response);
			return;
		}
		
		/**
		 * 根据用户ID获取用户的角色列表
		 * @param request
		 * @param response
		 */
		public void findByUid(HttpServletRequest request,HttpServletResponse response){
			String uid = request.getParameter("uid");
			if(uid != null && uid.length() > 0){
				UserRoleService userRoleService = (UserRoleService)this.getApplicationContext().getBean("userRoleService");
				List<UserRole> list = userRoleService.findByUid(uid);
				
				Map rs = new HashMap();
				rs.put("success", true);
				rs.put("items", list);
				rtn(rs, request, response);
				return;
			}
			
			Map rs = new HashMap();
			rs.put("success", false);
			rtn(rs, request, response);
			return;
		}
}
